package com.doraro.platform.module.user.controller;


import com.doraro.platform.common.utils.Result;
import com.doraro.platform.module.user.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author doraro
 * @since 2019-05-16
 */
@RestController
@RequestMapping("/api/sysRole")
@Api(description = "角色相关接口", tags = "sys_role")
public class SysRoleRestController {
    @Autowired
    private ISysRoleService roleService;

    @GetMapping("/list")
    @ApiOperation("获取角色列表")
    @RequiresPermissions("sysRole:list")
    public Result getByPage() {
        return new Result().ok().addData("list", roleService.listWithMenus());
    }

    @GetMapping("/info")
    @ApiOperation("根据角色id获取角色详情")
    @RequiresPermissions("sysRole:info")
    public Result getRoleInfo(@RequestParam Integer id) {
        Set<String> perms = roleService.getPemsByRoleIds(Collections.singletonList(id));
        return new Result().ok().addData("info", perms);
    }


    @PutMapping("/menuPerm/{roleId}")
    @ApiOperation("根据菜单id修改角色权限")
    @RequiresPermissions("sysRole:menuPerm")
    public Result changeRolePermByMenuIds(@RequestBody List<Integer> menuIds, @PathVariable Integer roleId) {
        roleService.changeRolePermByMenuIds(roleId, menuIds);
        return new Result().ok();
    }


}

